// pages/navbar/navbar.js
import { apiGetSchoolAndCommentList } from '../../utils/http/api'
import click from "../../utils/click";
const dictionary = wx.getStorageSync('dictionary')

Component({

  properties: {
    islogo: Boolean,
    isShare: {
      type: Boolean,
      value: false
    }
  },

  stickyProps: {
    zIndex: 2,
  },

  /**
   * 页面的初始数据
   */
  data: {
    dictionary,
    visibleNav: false, // 搜索显示隐藏
    valueNav: '', // 搜索框值
    datas: [],
    loadingFlag: false,
    currIndex:0,
    pageNum:1,
    pageSize:20,
    windowHeight:0
  },


  methods: {

    // 节流搜索
  handleChange: click.throttle(
    function(e){
      // this.setData({
      //   dictionary: wx.getStorageSync('dictionary')
      // })
      wx.showLoading({
        title: '正在加载',
      });
      let value = e.detail.value
      let params = {
        keywords: value,
        num: 100000,
      }

      apiGetSchoolAndCommentList(params).then((res)=> {
        this.setData({
          datas: res
        })
        setTimeout(()=> {
          wx.hideLoading()
        },1000)

        // this.setData({
        //   loadingFlag: false
        // })
      })
    },
    1000
  ),
  

    goBack() {
      wx.navigateBack({
        delta: 1 // 返回的页面数，如果 delta 大于现有页面数，则返回到首页。默认为1。
      })
    },

    // 显示搜索
  openSwitch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 关闭搜索
  onClose() {
    this.setData({
      visibleNav: false,
    });
    wx.showTabBar()
  },

  // 搜索
  switchInt() {
    //判断是否为空
    if(this.data.valueNav==''){
      wx.showToast({
        title: '请输入关键字',
      })
    }
    let params = {
      keywords: this.data.valueNav,
      searchType:Number(this.data.currIndex),
      page:this.data.pageNum,
      pageSize:this.data.pageSize
    }

    apiGetSchoolAndCommentList(params).then((res)=> {
      this.setData({
        datas: res
      })
    })

  },

  goSchools(e) {
    // 值不清
    // this.setData({
    //   valueNav: '',
    //   datas: {}
    // })
    let schoolId = e.currentTarget.dataset.ids
    wx.navigateTo({
      url: '/pages/schools/schools?schoolId=' + schoolId,
    })
    // 不关闭
    // this.onClose()
  },

  goIndex(e) {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  goComments(e) {
    // this.setData({
    //   valueNav: '',
    //   datas: {}
    // })
    let commentId = e.currentTarget.dataset.ids
    wx.navigateTo({
      url: '/pages/commentDetails/commentDetails?commentId=' + commentId,

    })
    // this.onClose()
  },

  onTabsChange(event) {
    this.setData({
      currIndex:event.detail.value
    })
    console.log(this.data.currIndex)
  },

  onTabsClick(event) {
    console.log(`Click tab, tab-panel value is ${event.detail.value}.`);
  },

  onStickyScroll(event) {
    console.log(event.detail);
  },



  

}

})